<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="医生姓名" prop="doctorName">
				<el-input
					v-model="queryParams.doctorName"
					placeholder="请输入医生姓名"
					clearable
					size="small"
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="签约状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择签约状态" clearable size="small">
					<el-option label="待医生签署" :value="0"/>
					<el-option label="待后台签署" :value="1"/>
					<el-option label="合同待归档" :value="2"/>
					<el-option label="合同已归档" :value="3"/>
					<el-option label="合同已撤回" :value="4"/>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-table v-loading="loading" :data="contractList">
			<el-table-column label="序号" type="index" :index="indexMethod" align="center"/>
			<el-table-column label="合同名称" align="center">
				<template slot-scope="scope">
					<el-button type="text" size="mini" @click="viewContract(scope.row.previewUrl)">
						{{ scope.row.contractName }}
					</el-button>
				</template>
			</el-table-column>
			<el-table-column label="医生姓名" align="center" prop="doctorName"/>
			<el-table-column label="签约状态" align="center" prop="status">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.status == 0" type="danger">待医生签署</el-tag>
					<el-tag v-if="scope.row.status == 1" type="success">待后台签署</el-tag>
					<el-tag v-if="scope.row.status == 2">合同待归档</el-tag>
					<el-tag v-if="scope.row.status == 3" type="warning">合同已归档</el-tag>
					<el-tag v-if="scope.row.status == 4" type="info">合同已撤回</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="合同发送时间" prop="createTime" align="center"/>
			<el-table-column label="合同签署时间" prop="signTime" align="center"/>
			<el-table-column label="操作" align="center">
				<template slot-scope="scope">
					<el-dropdown>
					    <span class="el-dropdown-link">
							操作<i class="el-icon-arrow-down el-icon--right"></i>
					    </span>
						<el-dropdown-menu slot="dropdown">
							<el-dropdown-item @click.native="extSign(scope.row)" :disabled="scope.row.status!=1"><i class="el-icon-edit"></i>签署</el-dropdown-item>
							<el-dropdown-item @click.native="contractFiling(scope.row)" :disabled="scope.row.status!=2"><i class="el-icon-paperclip"></i>归档</el-dropdown-item>
							<el-dropdown-item @click.native="downloadContract(scope.row)"><i class="el-icon-download"></i>下载</el-dropdown-item>
							<el-dropdown-item @click.native="recallContract(scope.row)" divided><i class="el-icon-refresh-left"></i>撤回</el-dropdown-item>
						</el-dropdown-menu>
					</el-dropdown>
				</template>
			</el-table-column>
		</el-table>

		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>
	</div>
</template>

<script>
import {
	listContract,
	exportContract,
	updateContract
} from "@/api/sunshine/contract";
import { extSign, querySignResult, contractFiling } from '@/api/sunshine'
import { mapGetters } from 'vuex'

export default {
	name: "Contract",
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 合同医生关联表格数据
			contractList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				contractId: null,
				doctorId: null,
				contractName: null,
				doctorName: null,
				status: null,
				orderByColumn: 'create_time',
				isAsc: 'desc'
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
			transactionId: undefined,
			currentId: undefined,
			contractId: undefined
		};
	},
	created() {
		this.getList();
		document.addEventListener('visibilitychange', this.handleVisibility)
	},
	destroyed() {
		document.removeEventListener('visibilitychange', this.handleVisibility)
	},
	computed: {
		...mapGetters(['customerId'])
	},
	methods: {
		/** 查询合同医生关联列表 */
		getList() {
			this.loading = true;
			listContract(this.queryParams).then(response => {
				this.contractList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				contractDoctorId: null,
				contractId: null,
				doctorId: null,
				contractName: null,
				doctorName: null,
				status: "0",
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.queryParams.status = undefined;
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.contractDoctorId)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加合同医生关联";
		},
		viewContract(previewUrl) {
			window.open(previewUrl, "_blank")
		},
		/** 导出按钮操作 */
		handleExport() {
			const queryParams = this.queryParams;
			this.$confirm('是否确认导出所有合同医生关联数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return exportContract(queryParams);
			}).then(response => {
				this.download(response.msg);
			}).catch(function () {
			});
		},
		indexMethod(index) {
			return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
		},
		downloadContract(row) {
			window.open(row.downloadUrl, "_blank")
		},
		extSign(row) {
			this.currentId = row.contractDoctorId;
			this.contractId = row.contractId;
			let params = {
				customerId: this.customerId, signType: 'adminSignaturePositions',
				contractId: this.contractId, docTitle: row.contractName
			}
			extSign(params).then(res => {
				this.transactionId = res.data.transactionId;
				window.open(res.data.extSignUrl, "_blank")
			})
		},
		handleVisibility(e) {
			console.log(e)
			if (!e.target.hidden && this.transactionId) {
				this.querySignResult();
			}
		},
		querySignResult() {
			let params = {
				customerId: this.customerId, contractId: this.contractId,
				transactionId: this.transactionId
			}
			querySignResult(params).then(res => {
				if(res.code == 200 && res.data.result === "3000") {
					this.transactionId = undefined;
					this.msgSuccess( '签章成功')
					updateContract({contractDoctorId: this.currentId, status: 2}).then(response => {
						if(response.code == 200) {
							this.getList();
						}
					})
				}
			}).catch(error => {
				console.log(error);
				this.transactionId = undefined;
			})
		},
		contractFiling(row) {
			this.currentId = row.contractDoctorId;
			this.contractId = row.contractId;
			contractFiling({contractId: this.contractId}).then(res => {
				if(res.code == 200) {
					updateContract({contractDoctorId: this.currentId, status: 3}).then(response => {
						if(response.code == 200) {
							this.getList();
						}
					})
					this.msgSuccess( '您的操作已提交成功')
				}
			})
		},
		recallContract(row) {
			this.$confirm('您确定要撤回该合同吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				updateContract({contractDoctorId: row.contractDoctorId, status: 4}).then(res => {
					if (res.code === 200) {
						this.$message.success('操作成功');
						this.getList();
					}
				})
			})
		}
	}
};
</script>

<style lang="scss" scoped>
	.el-dropdown-link {
		cursor: pointer;
		color: #409EFF;
	}
	.el-icon-arrow-down {
		font-size: 12px;
	}
</style>
